import { defineStore } from 'pinia'
import {
  getUserToken,
  getNoTokenLogin,
  getCoreSession,
  checAuth,
  getPermission
} from '@/api/account'
import { parseUrlParams } from '@/utils/tools.js'

import crypto from '@/utils/crypto'
export const useAccountStore = defineStore('account', {
  state: () => ({
    FETCHEDPERMISSION: false, // cc登录是否请求过一次权限。
    TOKEN: '',
    USER_ID: '',
    SESSION: '', // 核心登录返回的session,供打开核心页面拼接url使用
    DESENSITIZATION: false, // 个人主页数据是否脱敏
    PERMISSION_LIST: []
  }),
  getters: {
    token: (state) => state.TOKEN
  },
  actions: {
    getToken() {
      let qs = parseUrlParams(window.location.href)
      const { userId } = qs
      this.USER_ID = userId
      const encryptContent = crypto.set(userId)
      const data = {
        userId: userId || this.USER_ID,
        encryptContent
      }
      return new Promise((resolve, reject) => {
        getUserToken(data)
          .then((res) => {
            const TOKEN = `${res.data}`
            this.TOKEN = TOKEN
            this.getPermissionList()
            resolve(TOKEN)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    getCCWebToken() {
      let qs = parseUrlParams(window.location.href)
      const { account, signature } = qs
      this.ACCOUNT = account
      // const encryptContent = crypto.set(account)
      const data = {
        account: account || this.ACCOUNT,
        grantType: 'free',
        encryptContent: signature
      }
      return new Promise((resolve, reject) => {
        getNoTokenLogin(data)
          .then((res) => {
            const TOKEN = `${res.data.token}`
            this.TOKEN = TOKEN
            this.USER_ID = res.data.userId
            this.getPermissionList()

            resolve(TOKEN)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    getPermissionList() {
      return new Promise((resolve, reject) => {
        getPermission()
          .then((res) => {
            const PERMISSION_LIST = res.data.resourceList || []
            this.PERMISSION_LIST = PERMISSION_LIST
            console.log('this.PERMISSION_LIST', PERMISSION_LIST)
            resolve(PERMISSION_LIST)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    getSession() {
      const params = {
        userId: this.USER_ID
      }
      if (this.SESSION) {
        return this.SESSION
      }
      return new Promise((resolve, reject) => {
        getCoreSession(params)
          .then((res) => {
            const SESSION = `${res.data}`
            this.SESSION = SESSION
            resolve(SESSION)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    checAccountAuth() {
      let qs = parseUrlParams(window.location.href)
      const { insuredId } = qs
      const params = {
        insuredId
      }
      return new Promise((resolve, reject) => {
        checAuth(params)
          .then((res) => {
            this.FETCHEDPERMISSION = true
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    resetToken() {
      this.TOKEN = null
      // _removeLocal('AUTH_TOKEN')
    },
    setHomeDesensitization(val) {
      this.DESENSITIZATION = val
    }
  }
})
